/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package e.z.blog.back.com.impl;

import e.z.blog.back.com.ComDataTableApi;
import java.lang.reflect.Field;
import java.util.*;
import org.nutz.dao.entity.annotation.Comment;
import org.nutz.ioc.loader.annotation.IocBean;
import z.h.w.jar.kit.Self;
import z.h.w.jar.kit.Text;
import z.h.w.jar.kit.To;

/**
 *
 * @author EnzoZhong
 */
@IocBean( singleton = false )
public class ComDataTable implements ComDataTableApi {

        private Map<String , Field> fieldMap;

        @Override
        public Map<String , Field> ini ( Class clazz , String... fieldNameArray ) {
                fieldMap = new LinkedHashMap();
                Stack<String> nameStack = To.objs2Stack( fieldNameArray );
                Stack<Field> fieldStack = Self.getFieldStack( clazz );

                while ( !fieldStack.isEmpty() ) {
                        Field field = fieldStack.pop();
                        for ( String name : nameStack ) {
                                String fieldName = field.getName().toLowerCase();
                                if ( fieldName.equalsIgnoreCase( name.toLowerCase() ) ) {
                                        field.setAccessible( true );
                                        fieldMap.put( name , field );
                                }
                        }
                }
                return fieldMap;
        }

        @Override
        public List<String> getValueList ( Object obj , String... fieldName ) {

                if ( fieldMap == null ) {
                        fieldMap = ini( obj.getClass() , fieldName );
                }
                List<String> stringList = new ArrayList();
                for ( String name : fieldName ) {
                        Field field = fieldMap.get( name );
                        System.out.println( name );
                        System.out.println( field );
                        String string;
                        try {
                                Object value = field.get( obj );
                                string = value.toString();
                        } catch ( IllegalArgumentException ex ) {
                                string = "IllegalArgumentException";
                        } catch ( IllegalAccessException ex ) {
                                string = "IllegalAccessException";
                        }
                        stringList.add( string );
                }
                return stringList;
        }

        @Override
        public String getTitleList ( Class clazz , String... fieldName ) {
                if ( fieldMap == null ) {
                        fieldMap = ini( clazz , fieldName );
                }
                List<Map<String , String>> mapList = new ArrayList();
                Collection<Field> fieldColl = fieldMap.values();
                for ( Field field : fieldColl ) {
                        HashMap<String , String> hashMap = new HashMap<String , String>();
                        String key = Text.toStr( "sTitle" );
                        String value = Text.toStr( field.getAnnotation( Comment.class ).value() );
                        hashMap.put( key , value );
                        mapList.add( hashMap );
                }
                Collections.reverse( mapList );
                return mapList.toString().replaceAll( "=" , ":" );
        }
}
